package cn.itsource.service.impl;

import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.RandomUtil;
import cn.itsource.basic.domain.AjaxResult;
import cn.itsource.domain.login.LoginInfo;
import cn.itsource.domain.user.User;
import cn.itsource.feign.zuul.LoginInfoFegin;
import cn.itsource.mapper.UserMapper;
import cn.itsource.service.UserService;
import cn.itsource.util.RedisService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

/**
 * @author: hong
 * @Description:
 * @Date: Created in 12:53 2022/2/25
 * @Modified: By:
 */
@Service("UserServiceImpl")
@Slf4j
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    RedisService redisService;
    @Autowired
    LoginInfoFegin userInfoFeignClient;

    /**
     * 手机号校验
     *
     * @param phone
     */
    @Override
    public void checkPhone(String phone) {
        //校验手机号
        Validator.validateMobile(phone, "手机号格式有误,请确认后重新输入");

        //判断手机号是否存在,如果 true ,存在,抛异常
        if (userMapper.findPhoneExist(phone)) throw new RuntimeException("该手机号已注册");

    }


    /**
     * 获取验证码
     *
     * @param phone
     */
    @Override
    public void sendPhoneCode(String phone) {
        //校验手机号
        Validator.validateMobile(phone, "手机号格式有误,请确认后重新输入");
        //判断key是否存在,如果存在说明用户五分钟内已经获取过验证码直接给用户返回
        String existcode = redisService.getKeyObjectValue(phone, String.class);
        if (Objects.isNull(existcode)) {
            //1.1不存在,生成code ,完成发送
            String code = RandomUtil.randomNumbers(4);
            //1.2将code存入redis
            redisService.setStringKeyAndValue(phone, code, 5);
            //1.3发送短信
            log.info("短信已发送,接受的手机号为{},短信的code码为:{}", phone, code);
        } else {
            //2.存在,直接取出code发送
            log.info("短信已发送,接受的手机号为{},短信的code码为:{}", phone, existcode);
        }

    }

    public void checkCode(String phone, String code) {
        String existCode = redisService.getKeyObjectValue(phone, String.class);
        //如果code不相等,或者不存在
        if (!Objects.equals(code, existCode)) {
            throw new RuntimeException("当前验证码不存在或已过期,请从新获取");
        }
    }


    /**
     * 前台用户注册的业务逻辑
     *
     * @param user
     */
    @Transactional
    @Override
    public AjaxResult insert(User user) {
        //判断两次密码是否一致
        if (!Objects.equals(user.getPassword(), user.getConfigPassword()))
            throw new RuntimeException("两次密码输入不一致,请确认后重新输入");

        //判断用户当前code是否存在
        this.checkCode(user.getPhone(), user.getCode());
        //初始化数据,加密密码,并且设置盐值,设置创建时间
//        String password = user.getPassword();
//        String salt = RandomUtil.randomString(6);
//        String s = MD5.create().setSalt(salt.getBytes()).digestHex(password);
//        user.setPassword(s);
        String initPassword = user.getPassword();
        user.init();
//        String Password  = user.getPassword();
//        user.setSalt(RandomUtil.randomNumbers(4));
//        String s = MD5.create().setSalt(user.getSalt().getBytes()).digestHex(Password);
//        user.setPassword(s);
        //存入数据库
        userMapper.insert(user);
        //x新增数据到userinfo表
        user.setInitPassword(initPassword);
        userInfoFeignClient.logininfoAdd(user);
        return AjaxResult.me();
    }


    /**-------------------------------------------private -----------------------*/

}
